package com.bw.shop2208a.utils

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bw.shop2208a.R

object GlideUtils {
    /**
     * context :Context可以避免内存泄漏
     */
    fun loadImage(context: Context, imageView: ImageView?, url: String) {
        if (imageView != null) {
            Glide.with(context)
                .load(url)
                .error(R.mipmap.ic_launcher)//图片网址错误
                .placeholder(R.drawable.ic_launcher_background)//占位符
                .skipMemoryCache(false)//是否跳过缓存
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                .thumbnail(0.1f)//缩略图
                .centerCrop()//居中
                .into(imageView)
        }
    }

    fun loadCircleImage(context: Context, imageView: ImageView, url: String) {
        Glide.with(context)
            .load(url)
            .error(R.mipmap.ic_launcher)//图片网址错误
            .placeholder(R.drawable.ic_launcher_background)//占位符
            .skipMemoryCache(false)//是否跳过缓存
            .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
            .thumbnail(0.1f)//缩略图
            .centerCrop()//居中
            .transform(CenterCrop(), CircleCrop())
            .into(imageView)
    }


    fun loadConnerImage(context: Context, imageView: ImageView, url: String, raduis: Int) {
        Glide.with(context)
            .load(url)
            .error(R.mipmap.ic_launcher)//图片网址错误
            .placeholder(R.drawable.ic_launcher_background)//占位符
            .skipMemoryCache(false)//是否跳过缓存
            .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
            .thumbnail(0.1f)//缩略图
            .centerCrop()//居中
            .transform(CenterCrop(), RoundedCorners(raduis))
            .into(imageView)
    }
}